import { Inject, Controller, Get, Post, Patch, Body } from '@midwayjs/core';
import { httpResult } from '../utils/http-result';
import { CompanyService } from '../service/company.service';
import { RedisService } from '@midwayjs/redis';
import { isPhone, isSMS } from '../utils/rg';

@Controller('/company')
export class CompanyController {
  @Inject()
  companyService: CompanyService;

  @Inject()
  redisService: RedisService;

  @Get('/company')
  async companyInfo(@Body() body) {
    return body;
  }

  @Post('/company')
  async company(@Body() body) {
    const { phone, sms, wx_open_id } = body;
    console.log('body', body);
    if (!isPhone(phone)) {
      return httpResult.error('手机号错误');
    }

    if (!isSMS(sms)) {
      return httpResult.error('请输入正确的验证码');
    }

    const existSMS = true;
    if (!existSMS) return httpResult.error('验证码有误');

    const res = await this.companyService
      .createCompany(phone, wx_open_id)
      .catch(err => console.log(err));
    // 走微信绑定方式
    return httpResult.success({ res }, '注册成功');
  }

  @Get('/getSMS')
  async getRegisterSMS() {}

  @Patch('user12')
  async updateUser() {}
}
